package com.keyingbo.pattern.dynamic;

import com.keyingbo.leran.pattern.proxy.mydynamic.MyClassLoader;
import com.keyingbo.leran.pattern.proxy.mydynamic.MyInvocationHandler;
import com.keyingbo.leran.pattern.proxy.mydynamic.MyProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Auther: KEYB
 * @Date: 2019-3-14 00:09
 * @Description:
 */
public class LifeProxy implements MyInvocationHandler {
    private Object target;

    public Object getInstance(Object lifeObject) throws Exception {
        this.target = lifeObject;
        Class<?> clazz = target.getClass();
        return MyProxy.newProxyInstance(new MyClassLoader(lifeObject.getClass()), clazz.getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(this.target, args);
        after();
        return obj;
    }

    private void before() {
        System.out.println("代理前....");
    }

    private void after() {
        System.out.println("代理后....");
    }
}
